<template>

  <el-container class="home-container">
    <!--头部-->
    <el-header>
      <div>
        <span>电商管理系统</span>
      </div>
      <el-button type="info" round @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!--侧边栏-->
      <el-aside :width="isCollapse?'18px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!--侧边栏菜单区域-->
        <el-menu
          default-active="2"
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath">
          <!--一级菜单-->
          <el-submenu :index="item.psId +''" v-for="item in menusList" v-bind:key="item.psId">
            <!--一级菜单的模板区域-->
            <template slot="title">
              <i :class="iconItems[item.psId]"></i>
              <span>{{item.psName}}</span>
            </template>

            <el-menu-item :index="'/' + subItem.psC"
                          v-for="subItem in item.children"
                          :key="subItem.psId" @click="saveNavState('/'+subItem.psC)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.psName}}</span>
              </template>
            </el-menu-item>

           <!-- <el-menu-item-group>
              <template slot="title">分组一</template>
              <el-menu-item index="1-1">选项1</el-menu-item>
              <el-menu-item index="1-2">选项2</el-menu-item>
            </el-menu-item-group>
            <el-menu-item-group title="分组2">
              <el-menu-item index="1-3">选项3</el-menu-item>
            </el-menu-item-group>-->
            <!--三级菜单-->
          <!--  <el-submenu index="1-4">
              <template slot="title">选项4</template>
              <el-menu-item index="1-4-1">选项1</el-menu-item>
            </el-submenu>-->
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!--路由配置-->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  export default {
    data(){
       return{
         //左侧菜单数据
         menusList:[],
         //自定义菜单icon
         iconItems:{
           "101":"el-icon-s-goods",
           "102":"el-icon-s-order",
           "103":"el-icon-s-management",
           "125":"el-icon-s-custom",
           "145":"el-icon-s-data"
         },
         //默认不折叠
         isCollapse:false,
         //激活的链接地址
         activePath:''
       }
    },
    //初始化加载列表
    created() {
      this.getMenusList();
      this.activePath = window.sessionStorage.getItem("activePath")
    },
    methods: {
      logout() {
        //清空缓存
        window.sessionStorage.clear();
        //跳转指定页面
        this.$router.push("/login");
      },
      async getMenusList(){
        const {data:res} = await this.$axios.get("menus")
        if(res.code == 200) {
          this.menusList = res.data;
          //console.log(this.menusList);
        }
      },
      //点击按钮，切换菜单折叠收起
      toggleCollapse() {
         this.isCollapse = !this.isCollapse;
      },
      //保存点击激活的链接
      saveNavState(activePath){
        window.sessionStorage.setItem("activePath",activePath);
        this.activePath = activePath;
      }
    }

  }

</script>

<style type="text/css" scoped>
  .home-container{
    height:100%;
  }
  .el-header{
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    color: #ffff;
    font-size: 20px;
  }
  .el-aside{
    background-color: #333744;
  }
  .el-menu{
    border-right: none;
  }
  .el-main{
    background-color: #eaedf1;
  }
  .toggle-button{
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2px;
    cursor: pointer;
  }
</style>
